package com.egao.oa.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.oa.biz.JournalBiz;
import com.egao.oa.entity.Journal;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-03-22 15:52:42
 * @version ：1.0
 */
@Repository
public class JournalBizImpl extends BaseDao implements JournalBiz {

	@Override
    public JsonResult list(PageResult2<T> page, Journal journal, User loginUser) throws Exception {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<Journal> rm = new BeanPropertyRowMapper<Journal>(Journal.class);
        StringBuffer sql = new StringBuffer("select j.id,j.organization_id,j.content");
        sql.append(",j.keywords,j.schedule,j.type,j.state,j.remark");
		sql.append(",j.create_time,j.create_user,j.update_time,j.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
        sql.append(" from oa_journal j ");
        sql.append(" inner join sys_user suser on suser.user_id=j.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=j.organization_id ");
        sql.append(" where j.deleted=0 and sogzn.deleted=0 ");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and j.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(loginUser.getUserId() != UserConstant.ADMIN_ID && loginUser.getUserId() != 5){
            sql.append(" and (j.create_user=:create_user or j.type='会议记录') ");
            map.put("create_user", loginUser.getUserId() + "");
        }
        if(Utils.isNotEmpty(journal)){
//			if(Utils.isNotEmpty(journal.getName())){
//				sql.append(" and instr(j.name,:name)>0 ");
//				map.put("name", journal.getName());
//			}
            if(Utils.isNotEmpty(journal.getSort())&&Utils.isNotEmpty(journal.getOrder())){
                sql.append(" order by j."+journal.getSort()+" "+journal.getOrder());
            } else {
                sql.append(" order by j.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<Journal> list(Journal journal, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<Journal> rm = new BeanPropertyRowMapper<Journal>(Journal.class);
        StringBuffer sql = new StringBuffer("select j.id,j.organization_id,j.content");
        sql.append(",j.keywords,j.schedule,j.type,j.state,j.remark");
		sql.append(",j.create_time,j.create_user,j.update_time,j.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
        sql.append(" from oa_journal j ");
        sql.append(" inner join sys_user suser on suser.user_id=j.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=j.organization_id ");
        sql.append(" where j.deleted=0 and sogzn.deleted=0 ");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and j.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(journal)){
//			if(Utils.isNotEmpty(journal.getName())){
//				sql.append(" and instr(j.name,:name)>0 ");
//				map.put("name", journal.getName());
//			}
            if(Utils.isNotEmpty(journal.getSort())&&Utils.isNotEmpty(journal.getOrder())){
                sql.append(" order by j."+journal.getSort()+" "+journal.getOrder());
            } else {
                sql.append(" order by j.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(),map, rm);
    }

    @Override
    public Journal getById(Journal journal) {
        RowMapper<Journal> rm = new BeanPropertyRowMapper<Journal>(Journal.class);
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select j.id,j.organization_id,j.content");
        sql.append(",j.keywords,j.schedule,j.type,j.state,j.remark");
		sql.append(",j.create_time,j.create_user,j.update_time,j.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
        sql.append(" from oa_journal j ");
        sql.append(" inner join sys_user suser on suser.user_id=j.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=j.organization_id ");
        sql.append(" where j.deleted=0 and sogzn.deleted=0 and j.id=:id ");
        map.put("id", journal.getId());
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(Journal journal, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("insert into oa_journal (");
        sql.append("organization_id,content,keywords,schedule,type");
		sql.append(",state,remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:content,:keywords,:schedule,:type");
		sql.append(",:state,:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("content", journal.getContent());
        map.put("keywords", journal.getKeywords());
        map.put("schedule", journal.getSchedule());
        map.put("type", journal.getType());
        map.put("state", journal.getState());
        map.put("remark", journal.getRemark());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(Journal journal, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("update oa_journal set ");
        sql.append("content=:content,schedule=:schedule,type=:type ");
		sql.append(",keywords=:keywords ");
		sql.append(",state=:state,remark=:remark,update_time=now() ");
		sql.append(",update_user=:update_user where id=:id ");
        map.put("content", journal.getContent());
        map.put("schedule", journal.getSchedule());
        map.put("type", journal.getType());
        map.put("keywords", journal.getKeywords());
        map.put("state", journal.getState());
        map.put("remark", journal.getRemark());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", journal.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(Journal journal, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if(journal.getId().length()>0){
            for(String ID : journal.getId().split(",")){
                map  = new HashMap<String, String>();
                if(ID==null || ID.equals("")){ break; }
                StringBuffer sql =new StringBuffer("update oa_journal set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", ID);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(),map);
            }
        }
        return count > 0;
    }
}